<template>
  <transition name="msg-box-fade">
    <div v-if="visible"
         class="msg-box__wrapper">
      <div class="msg-box"
           @click.stop>
        <h3 class="msg-box__title">{{ title }}</h3>
        <p class="msg-box__content">{{ content }}</p>
        <div class="msg-box__btns">
          <div v-if="type==='confirm'"
               class="msg-box-btn__cancle"
               @click="clickAction('cancel')">
            <p class="msg-box-btn__title msg-box-btn__border">{{ cancleTitle }}</p>
          </div>
          <div class="msg-box-btn__confirm"
               @click="clickAction('confirm')">
            <p class="msg-box-btn__title">{{ confirmTitle }}</p>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  data () {
    return {
      visible: false, // 用于控制整个窗口的显示/隐藏
      title: '操作提示', // 提示框标题
      content: '提示框的内容', // 提示框的内容
      cancleTitle: '取消', // 取消按钮的文字
      confirmTitle: '确认', // 确认按钮的文字
      type: 'confirm', // 表明弹框的类型：confirm - 确认弹窗（有取消按钮）；alert - 通知弹框（没有取消按钮）
    }
  },
  methods: {
    show (content, config) {
      this.content = content || '提示框的内容'
      if (Object.prototype.toString.call(config) === '[object Object]') {
        // 确保用户传递的是一个对象
        this.title = config.title || '操作提示'
        this.cancleTitle = config.cancleTitle || '取消'
        this.confirmTitle = config.confirmTitle || '确认'
        this.type = config.type || 'confirm'
      }
      this.visible = true
    },
    hidden () {
      this.visible = false
      this.title = '操作提示'
      this.cancleTitle = '取消'
      this.confirmTitle = '确认'
      this.type = 'confirm'
    },
    clickAction (type) {
      this.$emit('clickAction', type)
      this.hidden()
    }
  }
}
</script>

<style lang="less" scoped>
.msg-box__wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 500;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}


.msg-box {
  position: absolute;
  top: 28%;
  left: 0;
  right: 0;
  width: 280px;
  margin: 0 auto;
  background-color: #fff;
  border-radius: 5px;
  z-index: 999;
  user-select: none;
  /* 顶部标题部分 */
  .msg-box__title {
    padding-top: 20px;
    text-align: center;
    font-size: 20px;
    font-weight: 500;
    color: #333;
  }

  /* 中间内容部分 */
  .msg-box__content {
    padding: 0 15px;
    padding-top: 20px;
    margin-bottom: 32px;
    text-align: center;
    font-size: 16px;
    color: #666;
    line-height: 1.5;
  }

  /* 底部按钮样式 */
  .msg-box__btns {
    display: flex;
    border-top: 1px solid #eee;
    .msg-box-btn__cancle,
    .msg-box-btn__confirm {
      flex: 1;
    }
    .msg-box-btn__confirm {
      color: #8c34ac;
    }
    .msg-box-btn__title {
      text-align: center;
      font-size: 16px;
      margin: 14px 0;
      padding: 6px 0;
      &.msg-box-btn__border {
        border-right: 1px solid #eee;
      }
    }
  }
}


.msg-box-fade-enter-active {
  animation: opacity 0.3s;
}
.msg-box-fade-enter-active .msg-box {
  animation: scale 0.3s;
}
.msg-box-fade-leave-active {
  animation: outOpacity 0.2s;
}

@keyframes opacity {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
@keyframes scale {
  0% {
    transform: scale(0);
  }
  60% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

/* 出去的动画 */
@keyframes outOpacity {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
</style>